﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MangaReader.MangaService;
using System.Diagnostics;

namespace MangaReader
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }
        MangaServiceClient service = new MangaServiceClient();
        MangaCollectionContract mangaList = new MangaCollectionContract();
        public static string[] listImages;

        void Form1_Load(object sender, EventArgs e)
        {
            // KissManga.GetWebContent("http://kissmanga.com/MangaList?page=1");
            mangaList = GetMangaList(1);
            this.numericUpDown1.Maximum = mangaList.TotalPage;
            ShowMangaList();
            this.Width = 580;
        }

        private MangaCollectionContract GetMangaList(int PageNumber)
        {
            return service.GetMangaCollection("kissmanga", "http://kissmanga.com/MangaList", PageNumber);
        }

        private void btnGetListFromPage_Click(object sender, EventArgs e)
        {
            mangaList = GetMangaList((int)this.numericUpDown1.Value);

            ShowMangaList();
        }

        private void ShowMangaList()
        {
            this.dgvMangaList.Rows.Clear();

            foreach (MangaStoryContract manga in mangaList.MangaList)
            {
                //Debug.WriteLine(manga.Title);
                this.dgvMangaList.Rows.Add(manga.Title, manga.Link, "View Detail");
            }
        }

        

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                try
                {
                    string urlTmp = "http://kissmanga.com/Manga/Nana-to-Kaoru?confirm=yes";
                    string url = this.dgvMangaList.Rows[e.RowIndex].Cells[1].Value.ToString();
                    MangaStoryDetailContract manga = service.GetMangaStoryDetail("kissmanga", url);
                    this.lblAuthor.Text = "Author: " + manga.Author;
                    this.txtSummary.Text = manga.Summary;
                    this.txtTitle.Text = this.dgvMangaList.Rows[e.RowIndex].Cells[0].Value.ToString();
                    this.imgMangaPicture.ImageLocation = manga.ImageLink;
                    this.txtAuthor.Text = manga.Author;
                    this.grbMangaDetail.Visible = true;
                    this.Width = 1100;
                    //DataGridViewLinkColumn links = new DataGridViewLinkColumn();

                    //links.UseColumnTextForLinkValue = true;
                    //links.HeaderText = "Title";
                    ////links.DataPropertyName = ColumnName.ReportsTo.ToString();
                    //links.ActiveLinkColor = Color.White;
                    //links.LinkBehavior = LinkBehavior.SystemDefault;
                    //links.LinkColor = Color.Blue;
                    //links.TrackVisitedState = true;
                    //links.VisitedLinkColor = Color.YellowGreen;
                    //links.Width = 300;
                    //links.ReadOnly = true;
                    //links.Resizable = DataGridViewTriState.False;

                    
                    //DataGridViewTextBoxColumn hideLinks = new DataGridViewTextBoxColumn();
                    //hideLinks.Visible = false;
                    //DataGridViewTextBoxColumn date = new DataGridViewTextBoxColumn();
                    //date.HeaderText = "Date";

                    //this.dgvChapterList.Columns.Add(links);
                    //this.dgvChapterList.Columns.Add(hideLinks);
                    //this.dgvChapterList.Columns.Add(date);
                    if (manga.Chapters != null)
                    {
                        this.dgvChapterList.Rows.Clear();
                        foreach (MangaChapter chapter in manga.Chapters)
                        {
                            this.dgvChapterList.Rows.Add(chapter.Title, chapter.Date, chapter.Link);
                        }
                    }
                    else
                    {
                        this.dgvChapterList.Rows.Clear();
                        MessageBox.Show("Can't load chapter list.");
                    }
                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }


                // MessageBox.Show(url);
            }

           
        }

        private void dgvChapterList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                try
                {
                    string strUrl = this.dgvChapterList.Rows[e.RowIndex].Cells[2].Value.ToString();
                    //listImages = service.GetDataForChapter("kissmanga", url);

                    #region "CODE FOR OPEN BROWSER"
                    //string strUrl = (string)this.dgvMangaList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                    try
                    {

                        System.Diagnostics.Process.Start(strUrl);

                    }

                    catch (Exception exc1)
                    {

                        // System.ComponentModel.Win32Exception is a known exception that occurs when Firefox is default browser.  

                        // It actually opens the browser but STILL throws this exception so we can just ignore it.  If not this exception,

                        // then attempt to open the URL in IE instead.

                        if (exc1.GetType().ToString() != "System.ComponentModel.Win32Exception")
                        {

                            // sometimes throws exception so we have to just ignore

                            // this is a common .NET bug that no one online really has a great reason for so now we just need to try to open

                            // the URL using IE if we can.

                            try
                            {

                                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("IExplore.exe", strUrl);

                                System.Diagnostics.Process.Start(startInfo);

                                startInfo = null;

                            }

                            catch (Exception exc2)
                            {

                                // still nothing we can do so just show the error to the user here.

                            }

                        }

                    }
                    #endregion

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
